package lc;

import java.util.Stack;

public class EvalReversePolish {
	public int evalRPN(String[] tokens) {
		Stack<Integer> stack = new Stack<>();
		for (String token : tokens) {
			int p, q;
			switch (token) {
			case "+":
				p=stack.pop();
				q=stack.pop();
				stack.push(q+p);
				break;
			case "-":
				p=stack.pop();
				q=stack.pop();
				stack.push(q-p);
				break;
			case "*":
				p=stack.pop();
				q=stack.pop();
				stack.push(q*p);
				break;
			case "/":
				p=stack.pop();
				q=stack.pop();
				stack.push(q/p);
				break;
			default:
				stack.push(Integer.parseInt(token));
				break;
			}
		}
		return stack.pop();
	}

	public static void main(String ...strings) {
		EvalReversePolish sol = new EvalReversePolish();
		int evalRPN = sol.evalRPN(new String [] {"2", "1", "+", "3", "*"});
		evalRPN = sol.evalRPN(new String [] {"4", "13", "5", "/", "+"});
	}
}
